/*
Schema.java

Copyright (c) 2007 Pontifical Catholic University of Rio de Janeiro, Brazil

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

package br.pucrio.inf.csm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * @author agazola
 *
 */
public final class Schema implements ISchema {

	private final List<Attribute> attributes;

	private final String name;
			
	public Schema(String name, List<Attribute> attributes) {
		if (attributes == null || attributes.isEmpty()) {
			throw new IllegalArgumentException("The schema must have at least "
					+ "one attribute specified.");
		}
		this.name = name;
		this.attributes = new ArrayList<Attribute>();
		Collections.copy(this.attributes, attributes);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see br.pucrio.inf.csa.schema.ISchema#getAttributes()
	 */
	public List<Attribute> getAttributes() {
		return Collections.unmodifiableList(attributes);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.pucrio.inf.csa.schema.ISchema#getSize()
	 */
	public int getSize() {
		return attributes.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.pucrio.inf.csa.schema.ISchema#getAttribute(int)
	 */
	public Attribute getAttribute(int index) {
		if (index < 0 || index >= attributes.size()) {
			throw new IllegalArgumentException(
					"Invalid index for schema attribute.");
		}
		return attributes.get(index);
	}
	
	public Attribute getAttributeByName(String name) {
		for (Attribute attr : attributes) {
			if (attr.getName().equals(name)) {
				return attr;
			}
		}
		return null; // TODO: melhorar isso
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.pucrio.inf.csa.schema.ISchema#getName()
	 */
	public String getName() {
		return name;
	}

	public int getAttributeIndex(Attribute attribute) throws SchemaException {
		for (int i = 0; i < attributes.size(); i++) {
			if (attribute.equals(attributes.get(i))) {
				return i;
			}
		}
		throw new SchemaException("The attribute " + attribute + " was not "
				+ "found in this schema.");
	}
	
	public boolean contains(Attribute attribute) {
		return attributes.contains(attribute);
	}

	/* (non-Javadoc)
	 * @see java.lang.Iterable#iterator()
	 */
	public Iterator<Attribute> iterator() {
		return attributes.iterator();
	}
	
	public Attribute getThesaurusAttribute() {
		for (Attribute attr : attributes) {
			if (attr.getType().equals(Attribute.Type.THESAURUS)) {
				return attr;
			}
		}
		return null;
	}

	public List<Attribute> getIdentityAttributes() {
		List<Attribute> identityAttributes = new ArrayList<Attribute>();
		for (Attribute attr : attributes) {
			if (attr.getType().equals(Attribute.Type.IDENTITY)) {
				identityAttributes.add(attr);
			}
		}
		return identityAttributes;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override	
	public String toString() {
		return name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((attributes == null) ? 0 : attributes.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Schema other = (Schema) obj;
		if (attributes == null) {
			if (other.attributes != null)
				return false;
		} else if (!attributes.equals(other.attributes))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}
